package io.renren.modules.file.controller;

import io.renren.common.utils.MongodbUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.WordUtils;
import io.renren.event.BaseEvent;
import io.renren.modules.file.entity.SysUploadFileEntity;
import io.renren.modules.file.service.SysUploadFileService;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.mq.ActiveMQProducerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 文档管理 上传 下载
 */
@RestController
@RequestMapping("file/sysuploadfile")
@Api(tags = "文档上传接口")
public class FileController extends AbstractController {

    @Autowired
    private SysUploadFileService sysUploadFileService;
    @Autowired
    private ActiveMQProducerService producer;
    @Autowired
    private ApplicationContext publisher;


    /**
     * 上传文件
     */
    @PostMapping(value = "/upload")
    //@PostMapping(value = "/upload", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    //@ApiParam(value = "上传的文件", required = true) MultipartFile multipartFile
    @ApiOperation("文件上传")
    private R uploadFile(
            //@ApiParam(value = "上传的文件") MultipartFile multipartFile
            @RequestParam(value = "file", required = false) MultipartFile multipartFile
    ) throws Exception {
        SysUploadFileEntity file = new SysUploadFileEntity();
        file.setUploadUser(getUserId());
        return sysUploadFileService.saveFile(multipartFile, file);
    }

    /**
     * 下载图片
     */
    @PostMapping("/downFile/{objectId}")
    @ApiOperation("下载")
    private void downFile(@PathVariable("objectId") String objectId, HttpServletRequest request, HttpServletResponse response) throws Exception {
        MongodbUtils.downFile(request, response, objectId);
    }

    /**
     * 刪除图片
     */
    @PostMapping(value = "/deleteFile/{objectId}")
    @ApiOperation("刪除")
    private R deleteFile(@PathVariable("objectId") String objectId) {
        producer.sendMessage(new ActiveMQQueue("myTest.queue"), "开始删除文件");
        Map<String, Object> eventMap = new HashMap<>();
        eventMap.put("eventName","开始删除文件");
        BaseEvent event = new BaseEvent(this, eventMap);
        publisher.publishEvent(event);

        return sysUploadFileService.deleteFile(objectId);
    }

    /**
     * 展示图片
     */
    @PostMapping("/showImage/{objectId}")
    @ApiOperation("showImage")
    private void test(@PathVariable("objectId") String objectId, HttpServletResponse response) throws Exception {
        MongodbUtils.showImage(response, objectId);
    }

    /**
     * 下载word
     */
    @PostMapping("/word")
    @ApiOperation("下载word")
    private R downloadWord(
            //@RequestBody Map<String, Object> map,
            HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, Object> map = new HashMap<>();
        //String reportImage = MongodbUtils.getImageInputStream(file.getObjectId());//"5c6e0c8dce923232e81ce64c"
        Map<String, String> imageMap = new HashMap<>();
        imageMap.put("image", "5c6e0c8dce923232e81ce64c");
        map.put("test", "test申请单13123");
        map.put("imageMap", imageMap);
        map.put("fileTitle", "测试哈哈哈.doc");
        map.put("ftlFile", "test.ftl");
        //WordUtils.exportMillCertificateWord("测试文件.docx", request, response, map);
        return sysUploadFileService.downLoadWord(map, request, response);
    }
}
